# -*- coding: utf-8 -*-
import scrapy
import re


class RenrenSpider(scrapy.Spider):
    name = 'renren'
    allowed_domains = ['renren.com']
    # start_urls = ['http://renren.com/']
    # 个人中心页网址
    start_urls = ['http://www.renren.com/972990680/profile']

    def start_requests(self):
        # 登录之后用 chrome 的 debug 工具从请求中获取的 cookies
        cookiesstr = "anonymid=k1k3dbbb-agm2kd; depovince=SC; _r01_=1; ick_login=8009ab90-bcee-4b66-a39a-208b0416f1a3; taihe_bi_sdk_uid=a1e989e60e98fefed9bf02390c322a82; taihe_bi_sdk_session=695deac8650095ce71781c7300428eb8; t=27e365438e0307349439554bae2a31746; societyguester=27e365438e0307349439554bae2a31746; id=974189336; xnsid=a327e337; jebecookies=01c7a5e2-1344-44d4-b412-016659e4de72|||||; ver=7.0; loginfrom=null; jebe_key=ffba3064-3d3a-4fc2-a35c-97662fa8c0bf%7C0831f3aac72c9344b63502c18078fcee%7C1586400601823%7C1%7C1586400602570"
        cookies = {i.split("=")[0]: i.split("=")[1] for i in cookiesstr.split("; ")}

        # 携带 cookies 的 Request 请求
        yield scrapy.Request(
            self.start_urls[0],
            callback=self.parse,
            cookies=cookies
        )

    def parse(self, response):
        # 从个人中心页查找关键词"闲欢"并打印
        print(re.findall("闲欢", response.body.decode()))
